package com.common.qq;

import android.app.Activity;

import com.common.util.LogUtil;
import com.tencent.connect.UserInfo;
import com.tencent.connect.auth.QQToken;
import com.tencent.connect.common.Constants;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * qq登录
 */
public class QQLoginUtil implements IUiListener {
    private QQListener qqListener;
    private static QQLoginUtil loginUtil;

    public static QQLoginUtil getInstance() {
        if (loginUtil == null) {
            synchronized (QQLoginUtil.class) {
                loginUtil = new QQLoginUtil();
            }
        }
        return loginUtil;
    }

    /**
     * qq登录授权
     *
     * @param tencent
     * @param activity
     * @param listener
     */
    public void login(Tencent tencent, Activity activity, QQListener listener) {
        qqListener = listener;
        if (!tencent.isSessionValid()) {
            tencent.login(activity, "all", this, true);
        }
    }

    /**
     * 获取个人信息
     *
     * @param activity
     * @param tencent
     * @param openId
     * @param access_token
     * @param express_in
     * @param listener
     */
    public void getQQUserInfo(Activity activity, Tencent tencent, final String openId, String access_token, String express_in, final QQListener listener) {
        tencent.setOpenId(openId);
        tencent.setAccessToken(access_token, express_in);
        QQToken token = tencent.getQQToken();
        UserInfo mInfo = new UserInfo(activity, token);
        mInfo.getUserInfo(new IUiListener() {
            @Override
            public void onComplete(Object object) {
                JSONObject jb = null;
                try {
                    jb = new JSONObject(object.toString());
                    listener.getUserInfoSuccess(jb.getString("nickname"), jb.getString("figureurl_qq_2"));
                } catch (JSONException e) {
                    e.printStackTrace();
                    listener.loginError("获取个人信息失败");
                }
            }

            @Override
            public void onError(UiError uiError) {
                listener.loginError(uiError.errorMessage);
            }

            @Override
            public void onCancel() {
                listener.loginError("用户取消");
            }
        });
    }

    @Override
    public void onComplete(Object o) {
        try {
            JSONObject object = new JSONObject(o.toString());
            qqListener.loginSuccess(object.getString("openid"), object.getString("access_token"), object.getString("expires_in"));
        } catch (JSONException e) {
            e.printStackTrace();
            qqListener.loginError("QQ登录授权失败");
        }
    }

    @Override
    public void onError(UiError uiError) {
        qqListener.loginError(uiError.errorMessage);
    }

    @Override
    public void onCancel() {
        qqListener.loginError("QQ登录取消");
    }
}
